<?php
$posts = Post::all(G::val($_REQUEST, 'page'), 1);

$pages = $posts['pages'];
$page = $posts['page'];

$total = count( $posts['rows'] );
$posts = $posts['rows'];

?>

<?php for($index=0; $index < $total; $index++):?>

	<?php if ($index % 3 == 0) { ?>
	    <div class="row">
	<?php } ?>


	<div class="span4">
	    <h2><a href="<?php echo url(ROUTE_HTML_APP, url_sfparams('chap0','user',array('u'=>$posts[$index]['user'])));?>">
	    	<?php echo $posts[$index]['user'] ?>
	    </a> 说</h2>
	    <p><small><?php echo date('Y-m-d H:i:s', $posts[$index]['time']) ?></small></p>
	    <p><?php echo $posts[$index]['post'] ?></p>
	</div>

	<?php if ($index % 3 == 2) { ?>
	    </div>
	<?php } ?>

<?php endfor;?>

<?php if ($total % 3 != 0) { ?>
  </div><!-- end row -->
<?php } ?>

<?php
function pageurl($page)
{
	$_REQUEST['page'] = intval($page);
	unset($_REQUEST['PHPSESSID']);
	return url(ROUTE_HTML_APP, url_params(
			G::val($_REQUEST,ROUTE_APP_KEY),
			G::val($_REQUEST,ROUTE_Q_KEY),
			G::val($_REQUEST,App::WIDGET_KEY),
			G::val($_REQUEST,App::AS_KEY),
			$_REQUEST
		) );
}
?>

<div class="pagination pagination-centered">
  <ul>
  	<?php if($page > 1):?>
    	<li><a href="<?php echo pageurl($page - 1)?>">上一页</a></li>
	<?php endif;?>
	
	<?php if ( $pages < 6): ?>
		<?php for($i=1;$i<=$pages;$i++): ?>
		<li <?php if($page == $i):?>class="active"<?php endif;?>><a href="<?php echo pageurl($i);?>"><?php echo $i;?></a></li>
		<?php endfor;?>
	<?php else:?>
		<?php 
		$i = ($pages-$page> 4) ? $page : ($pages-4);
		$end = $i + 5;
		for(;$i<$end;$i++): ?>
		<li <?php if($page == $i):?>class="active"<?php endif;?>><a href="<?php echo pageurl($i);?>"><?php echo $i;?></a></li>
		<?php endfor;?>

	<?php endif;?>

    <?php if($page < $pages):?>
  		<li><a href="<?php echo pageurl($page + 1)?>">下一页</a></li>
	<?php endif;?>

    <li class="disabled"><a href="javascript:;">总页数: <?php echo $pages?></a></li>
  </ul>
</div>